package com.wh.testCountDownLatch;

import java.util.concurrent.CountDownLatch;

//计数器(减法)----常见面试问题
//在多线程情况下，怎么才能保证主线程在所有子线程执行完毕后，才执行完成？
//用CountDownLatch 减法计数器，初始值设置子线程个数，主线程用countdownlatch.await() 等待计数器归0,主线程继续执行。
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        //总数是6
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for(int i=1;i<=6;i++){
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"--> go out");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println("close door");
    }
}

